home *** CD-ROM | disk | FTP | other *** search
- /*
- CDReadTOC - An XFCN to read the table of contents of a CD
- ©Apple Computer, Inc. 1988
- All Rights Reserved.
-
- 88/10/08 BL°B First Version
-
- To compile and link this file using Macintosh Programmer's Workshop,
-
- C -q2 CDReadTOC.c
- link -sn Main=CDReadTOC -sn STDIO=CDReadTOC ∂
- -sn INTENV=CDReadTOC -rt XFCN=42 ∂
- -m CDReadTOC CDReadTOC.c.o "{CLibraries}"CRuntime.o ∂
- "{CLibraries}"StdCLib.o ∂
- -o HyperCommands
-
- This link directive puts the XCMD in the file "HyperCommands".
- Substitute the name of the stack you want it in. To move XCMDs
- between stacks, use ResEdit. They can be in an individual stack,
- the Home stack, the HyperCard application, or the System File.
-
- */
-
- #include <cd.h>
-
- typedef struct {
- long minute;
- long second;
- long block;
- } TrackInfo;
-
- /* prototype definitions for functions */
- OSErr GetNumberTracks(short, short *);
- OSErr ReadTOC(short, short, TrackInfo *);
- void CFormatString(char *, long *, long);
-
- /* **** WARNING: DO NOT USE GLOBAL VARIABLES! **** */
-
-
- /************************************************************************
- *
- * Function: CDReadTOC
- *
- * Purpose: Read table of contents
- *
- * Returns: result of driver call to ReadTOC
- * normally 0, but could have parameter error or
- * other error if non-existent block is specified
- *
- * Side Effects:
- *
- * Description: We need no parameter:
- * Get the ioRefNum that we got from previously calling
- * CDOpen() by accessing the famous global.
- *
- ************************************************************************/
- pascal void
- CDReadTOC(paramPtr)
- XCmdBlockPtr paramPtr;
- {
- Str31 returnString;
- OSErr result;
- short ioRefNum;
- Handle refHandle;
-
- short numberTracks;
- TrackInfo **trackHandle;
-
- Handle formatString;
-
- /* Must be no parameter */
- if ((paramPtr->paramCount) != 0)
- {
- /* Report error in parameters by returning -1 */
- NumToStr(paramPtr, (long) -1, &returnString);
- paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &returnString);
- return;
- }
-
- /* Get the global ioRefNum and convert it. */
- refHandle = GetGlobal(paramPtr, GLOBALNAME);
- ioRefNum = atoi(*(refHandle));
- DisposHandle(refHandle);
- ioRefNum &= 0xFFFF; /* remove vRefNum; not needed. */
-
- result = GetNumberTracks(ioRefNum, &numberTracks);
-
- trackHandle = (TrackInfo **) 0; /* so final DisposHandle will work */
-
- if (result == noErr)
- {
- trackHandle = NewHandle(numberTracks * sizeof(TrackInfo));
- if (trackHandle == (TrackInfo **) 0)
- result = MemError();
- HLock(trackHandle);
- }
-
- if (result == noErr)
- result = ReadTOC(ioRefNum, numberTracks, *trackHandle);
-
- formatString = NewHandle(1000); /* we can 99 tracks * 9 bytes per track */
- if (formatString == nil)
- result = MemError();
-
- if (result == noErr)
- {
- HLock(formatString);
- CFormatString((char *)*formatString, *trackHandle, numberTracks * 3);
- HUnlock(formatString);
- paramPtr->returnValue = formatString; /* let HyperCard do the dispose */
- }
- else
- {
- /* We had an error. Convert result to string & return it */
- NumToStr(paramPtr, (long) result, &returnString);
- paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &returnString);
- if (formatString != nil)
- DisposHandle(formatString); /* only dispose of this if we aren't */
- /* returning it to Hypercard */
- }
-
- if (trackHandle != (TrackInfo **) 0)
- {
- HUnlock(trackHandle);
- DisposHandle(trackHandle);
- }
- }
-
-
-
- /************************************************************************
- *
- * Function: GetNumberTracks
- *
- * Purpose: report how many tracks are on this CD
- *
- * Returns: OSErr. Probably either
- * noErr everything's hunky-dory!
- * paramErr you messed up the call somehow.
- *
- * Side Effects: none
- *
- * Description: Simply call the driver.
- *
- ************************************************************************/
- OSErr
- GetNumberTracks(refNum, numberTracks)
- short refNum;
- short *numberTracks;
- {
- CDParam myPB;
- OSErr result;
-
- myPB.ioCompletion = 0;
- myPB.ioNamePtr = (char *) 0;
- myPB.ioVRefNum = 1;
- myPB.ioCRefNum = refNum;
- myPB.csCode = READTOC;
- myPB.csParam[0] = 0;
- myPB.csParam[1] = 1;
-
- result = PBControl(&myPB, false);
-
- if (result == noErr)
- *numberTracks = (short) BCD2DECIMAL(myPB.csParam[1]);
- return result;
- }
-
- /************************************************************************
- *
- * Function: ReadTOC
- *
- * Purpose: read table of contents
- *
- * Returns: OSErr
- * usually 0, but can be negative if driver returns
- * an error
- *
- * Side Effects: fills trackInfo with absolute minute, second, block
- * for each track.
- *
- * Description: We depend upon the trackInfo array already being
- * allocated by the calling routine.
- * Allocate the buffer to pass to
- * the READTOC call. Call the driver. Loop through
- * the READTOC buffer, converting values into the
- * final buffer.
- *
- ************************************************************************/
- OSErr
- ReadTOC(refNum, numberTracks, trackInfo)
- short refNum;
- short numberTracks;
- TrackInfo trackInfo[];
- {
- CDPlay3Param myPB;
- OSErr result;
- char *track;
- short trackSize;
- short i, j;
-
- trackSize = numberTracks * 4; /* 4 bytes per track in call */
- track = NewPtr(trackSize);
- myPB.ioCompletion = 0;
- myPB.ioNamePtr = (char *) 0;
- myPB.ioVRefNum = 1;
- myPB.ioCRefNum = refNum;
- myPB.csCode = READTOC;
- myPB.readType = BUFADDR;
- myPB.bufAddr = track;
- myPB.bufferLength = trackSize;
- myPB.track = 1;
-
- result = PBControl(&myPB, false);
- for (i = 0; i < numberTracks; i++)
- {
- j = i * 4;
- trackInfo[i].minute = (long) BCD2DECIMAL(track[j+1]);
- trackInfo[i].second = (long) BCD2DECIMAL(track[j+2]);
- trackInfo[i].block = (long) BCD2DECIMAL(track[j+3]);
- }
- DisposPtr(track);
- return result;
- }
-
-
- /************************************************************************
- *
- * Function: CFormatString
- *
- * Purpose: prepare return string
- *
- * Returns: nothing
- *
- * Side Effects: creates C string from input parameters
- *
- * Description: For each of the numArgs values in num[], convert the
- * value to an ascii string and concatenate.
- *
- ************************************************************************/
- void
- CFormatString(str, num, numArgs)
- char str[];
- long num[];
- long numArgs;
- {
- short i;
- char numStr[31];
-
- ltoa(num[0], str);
- for (i = 1; i < numArgs; i++)
- {
- strcat(str, ","); /* add comma to separate items */
- ltoa(num[i], numStr);
- strcat(str, numStr);
- }
- }
-
-
- /* C routines for HyperCard callbacks */
- #include <XCmdGlue.inc.c>
-